using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Crossborder.Core.Core;
using Crossborder.Core.Enum;
using StackExchange.Redis;

namespace Crossborder.Common.RedisModule
{
    public partial class RedisWorker : IRedisWorker
    {
        public string GetRedisString(string key)
        {
            return RedisCore.db.StringGet(key);
        }

        public async Task<string> GetRedisStringAsync(string key)
        {
            return await RedisCore.db.StringGetAsync(key);
        }

        public void SetRedisString(string key, string value, int ts, TimeSpanEnum spanEnum = TimeSpanEnum.Seconds)
        {
            TimeSpan expired = new();
            switch (spanEnum)
            {
                case TimeSpanEnum.Day:
                    expired = TimeSpan.FromDays(ts);
                    break;
                case TimeSpanEnum.Hours:
                    expired = TimeSpan.FromHours(ts);
                    break;
                case TimeSpanEnum.Minutes:
                    expired = TimeSpan.FromMinutes(ts);
                    break;
                case TimeSpanEnum.Seconds:
                    expired = TimeSpan.FromSeconds(ts);
                    break;
                default:
                    expired = TimeSpan.FromSeconds(ts);
                    break;
            }
            RedisCore.db.StringSet(key, value, expired);
        }

        public async Task SetRedisStringAsync(string key, string value, int ts, TimeSpanEnum spanEnum = TimeSpanEnum.Seconds)
        {
            TimeSpan expired = new();
            switch (spanEnum)
            {
                case TimeSpanEnum.Day:
                    expired = TimeSpan.FromDays(ts);
                    break;
                case TimeSpanEnum.Hours:
                    expired = TimeSpan.FromHours(ts);
                    break;
                case TimeSpanEnum.Minutes:
                    expired = TimeSpan.FromMinutes(ts);
                    break;
                case TimeSpanEnum.Seconds:
                    expired = TimeSpan.FromSeconds(ts);
                    break;
                default:
                    expired = TimeSpan.FromSeconds(ts);
                    break;
            }
            await RedisCore.db.StringSetAsync(key, value, expired);
            
        }
    }
}